package delphorm.web.taglibs;

import java.util.Iterator;

import delphorm.entite.questionnaire.FiltreNumerique;
import delphorm.entite.questionnaire.InstanceQuestion;
import delphorm.entite.questionnaire.InstanceReponse;
import delphorm.entite.questionnaire.InstanceReponseOuverte;
import delphorm.entite.questionnaire.Question;
import delphorm.entite.questionnaire.Reponse;
import delphorm.entite.questionnaire.ReponseOuverte;
import delphorm.entite.questionnaire.ValeurFichier;
import delphorm.entite.questionnaire.ValeurNumerique;
import delphorm.entite.questionnaire.ValeurTexte;


public class Toolz {
	public static boolean questionnaireEnCreation(Character c1)
	{
		return c1.equals(new Character('W'));
	}
	public static boolean questionnaireEnTest(Character c1)
	{
		return c1.equals(new Character('T'));
	}
	public static boolean questionnaireEnPerime(Character c1)
	{
		return c1.equals(new Character('O'));
	}
	public static boolean questionnaireEnPublication(Character c1)
	{
		return c1.equals(new Character('P'));
	}
	public static boolean reponseNumerique(Reponse r)
	{
		return r.getTypeDeDonnees().equals(new Character('N'));
	}
	public static boolean reponseTexte(Reponse r)
	{
		return r.getTypeDeDonnees().equals(new Character('T'));
	}
	public static boolean reponseFichier(Reponse r)
	{
		return r.getTypeDeDonnees().equals(new Character('F'));
	}
	public static String getTypeControl(Question q)
	{
		
		if(q.getOrdonnee().booleanValue() == true)
		{
			return new String("liste");
		}
		else if(q.getMaxReponses().intValue() == 1 &&
				   q.getMinReponses().intValue() == 1)
		{
			return new String("radio");
		}
		else if(q.getReponses().size() == 1 && 
				((Reponse)q.getReponses().get(0)).getType().equals("ouverte"))
		{
			return new String("aucun");
		}
		else
		{
			return new String("checkbox");
		}
	}
	
	public static String getTypeControl(Question q, Reponse r)
	{
		if(q!=null)
		{
			if(q.getOrdonnee().booleanValue() == true)
			{
				return new String("liste");
			}
			else if(q.getMaxReponses().intValue() == 1 &&
					   q.getMinReponses().intValue() == 1)
			{
						return new String("radio");
			}
			else if(q.getReponses().size() == 1 && 
					((Reponse)q.getReponses().get(0)).getType().equals("ouverte"))
			{
				return new String("aucun");
			}
			else
			{
				return new String("checkbox");
			}
		}
		if(r!=null)
		{
			if(	r.getType().equals("ouverte"))
			{
				return new String("aucun");
			}
			else 
			{
				return new String("checkbox");
			}
		}
		//TODO if q et r null exception
		return null;
	}
	
	
	public static boolean reponseRepondu(Reponse reponse, InstanceQuestion instanceQuestion)
	{
		Iterator itInstanceReponse = instanceQuestion.getInstancesReponse().iterator();
		while(itInstanceReponse.hasNext())
		{
			InstanceReponse instanceReponse = (InstanceReponse)itInstanceReponse.next();
			if(instanceReponse.getReponse().getId().equals(reponse.getId()))
			{
				return true;
			}
		}
		return false;
	}
	public static Object getValeurReponseOuverteRepondu(ReponseOuverte reponse, InstanceQuestion instanceQuestion)
	{
		Iterator itInstanceReponse = instanceQuestion.getInstancesReponse().iterator();
		while(itInstanceReponse.hasNext())
		{
			InstanceReponse instanceReponse = (InstanceReponse)itInstanceReponse.next();
			if(instanceReponse.getReponse().getId().equals(reponse.getId()))
			{
				InstanceReponseOuverte instanceReponseOuverte = (InstanceReponseOuverte)instanceReponse;
				if(reponse.getTypeDeDonnees().equals(new Character('N')))
				{
					ValeurNumerique valeur = (ValeurNumerique)instanceReponseOuverte.getValeur();
					return valeur.getValeur();
				}
				else if(reponse.getTypeDeDonnees().equals(new Character('T')))
				{
					ValeurTexte valeur = (ValeurTexte)instanceReponseOuverte.getValeur();
					return valeur.getValeur();
				}
				else if(reponse.getTypeDeDonnees().equals(new Character('F')))
				{
					ValeurFichier valeur = (ValeurFichier)instanceReponseOuverte.getValeur();
					return new String("<a href=\"telechargerValeurFichier.html?id="+valeur.getId()+"\">"+valeur.getNomFichier()+"</a>");
				}
			}
		}
		return null;
	}
	public static Integer getNumeroOrdreReponseRepondu(Reponse reponse, InstanceQuestion instanceQuestion)
	{
		Iterator itInstanceReponse = instanceQuestion.getInstancesReponse().iterator();
		while(itInstanceReponse.hasNext())
		{
			InstanceReponse instanceReponse = (InstanceReponse)itInstanceReponse.next();
			if(instanceReponse.getReponse().getId().equals(reponse.getId()))
			{
				return instanceReponse.getNumeroOrdre();
			}
		}
		return null;
	}
	public static boolean reponseNumeriqueEntiere(FiltreNumerique filtreNumerique)
	{
		if(filtreNumerique.getType() == null)
			return false;
		return filtreNumerique.getType().equals(new Character('E'));
	}
	public static boolean reponseNumeriqueReelle(FiltreNumerique filtreNumerique)
	{
		if(filtreNumerique.getType() == null)
			return false;
		
		return filtreNumerique.getType().equals(new Character('R'));
	}
	public static boolean valeurEntiere(Character c)
	{
		return c.equals(new Character('E'));
	}
	public static boolean valeurReelle(Character c)
	{
		return c.equals(new Character('R'));
	}
	public static boolean remplissagePartiel(Character c)
	{
		return c.equals(new Character('C'));
	}
	public static boolean remplissageCoherent(Character c)
	{
		return c.equals(new Character('O'));
	}
}
